﻿cmake_minimum_required(VERSION 3.15)

project(HandwritingVisualCNN LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17) # 设置C++标准为C++17
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /source-charset:utf-8 /execution-charset:gbk")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /source-charset:utf-8 /execution-charset:gbk")
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
	
find_package(OpenCV 4 REQUIRED )
include_directories(
    ${OpenCV_INCLUDE_DIRS}/include
    )
message(STATUS "-OpenCV_INCLUDE_DIRS--- = ${OpenCV_INCLUDE_DIRS}/include")

# LibTorch
set(LIBTORCH_DIR /usr/local/libtorch) # 根据实际情况修改路径
list(APPEND CMAKE_PREFIX_PATH ${LIBTORCH_DIR})
find_package(Torch REQUIRED)
include_directories(${TORCH_INCLUDE_DIRS})
message(STATUS "-TORCH_INCLUDE_DIRS--- = ${TORCH_INCLUDE_DIRS}/include")
# -------------- Compile CUDA for FP16 inference if needed  ------------------#
#option(USE_CUDA "Enable CUDA support" OFF)
find_package(CUDA QUIET)
if (CUDA_FOUND)
#    find_package(CUDA REQUIRED)
    include_directories(${CUDA_INCLUDE_DIRS})
    add_definitions(-DUSE_CUDA)
	set(USE_CUDA ON)
	message(STATUS "CUDA ON")
else ()
    set(USE_CUDA OFF)
	message(STATUS "CUDA OFF")
endif ()
add_executable(cvuiHandwritingPredication
    CNNHandwritingPredication.cpp
)

add_executable(CNNHWTrainDemo 
    CNNHandwritingTrainDemo.cpp
)
add_executable(CNNHandwritingTrain
    CNNHandwritingTrain.cpp
)
message(STATUS "-OpenCV_LIBS--- = ${OpenCV_LIBS}")
target_link_libraries(cvuiHandwritingPredication PRIVATE  ${OpenCV_LIBS} ${TORCH_LIBRARIES} )
target_link_libraries(CNNHandwritingTrain PRIVATE  ${OpenCV_LIBS}  ${TORCH_LIBRARIES} )
target_link_libraries(CNNHWTrainDemo PRIVATE  ${OpenCV_LIBS}  ${TORCH_LIBRARIES} )
if (USE_CUDA)
	target_compile_definitions(cvuiHandwritingPredication PRIVATE USECUDA_BOOL_CONST=1)
    target_compile_definitions(CNNHandwritingTrain PRIVATE USECUDA_BOOL_CONST=1)
    target_compile_definitions(CNNHWTrainDemo PRIVATE USECUDA_BOOL_CONST=1)
	message(STATUS "USECUDA_BOOL_CONST: 1")
else ()
    target_compile_definitions(cvuiHandwritingPredication PRIVATE USECUDA_BOOL_CONST=0)
    target_compile_definitions(CNNHandwritingTrain PRIVATE USECUDA_BOOL_CONST=0)
    target_compile_definitions(CNNHWTrainDemo PRIVATE USECUDA_BOOL_CONST=0)
	message(STATUS "USECUDA_BOOL_CONST: 0")
endif ()